CKUP_TIME=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="./backup/${BACKUP_TIME}"

echo "📦 开始备份监控系统数据..."

# 创建备份目录
mkdir -p ${BACKUP_DIR}

echo "📁 备份配置文件..."
# 备份核心配置文件
cp docker-compose.yml ${BACKUP_DIR}/
cp -r prometheus/ ${BACKUP_DIR}/
cp -r grafana/ ${BACKUP_DIR}/
cp -r scripts/ ${BACKUP_DIR}/
cp -r docs/ ${BACKUP_DIR}/

echo "💾 备份数据卷..."
# 备份Prometheus数据（如果存在）
if docker-compose ps prometheus | grep -q "Up"; then
    echo "备份Prometheus数据..."
    docker-compose exec -T prometheus tar czf - /prometheus > ${BACKUP_DIR}/prometheus_data.tar.gz 2>/dev/null || echo "Prometheus数据备份跳过"
fi

# 备份Grafana数据（如果存在）
if docker-compose ps grafana | grep -q "Up"; then
    echo "备份Grafana数据..."
    docker-compose exec -T grafana tar czf - /var/lib/grafana > ${BACKUP_DIR}/grafana_data.tar.gz 2>/dev/null || echo "Grafana数据备份跳过"
fi

# 创建备份信息文件
echo "备份时间: ${BACKUP_TIME}" > ${BACKUP_DIR}/backup_info.txt
echo "项目版本: v1.0" >> ${BACKUP_DIR}/backup_info.txt
docker --version >> ${BACKUP_DIR}/backup_info.txt
docker-compose --version >> ${BACKUP_DIR}/backup_info.txt

echo "✅ 备份完成！"
echo "📂 备份位置: ${BACKUP_DIR}"
echo "📊 备份内容:"
ls -la ${BACKUP_DIR}/
